#!/bin/bash

# 镜像脚本地址
centos_base="https://gitee.com/homevip/note/raw/master/script/sources-base/centos-base.sh"
ubuntu_base="https://gitee.com/homevip/note/raw/master/script/sources-base/ubuntu-base.sh"

# 获取系统类型 1:Ubuntu 2:CentOS 3:Deepin
get_os(){
    # Ubuntu
    if [ -f /etc/os-release ] && grep -q "UBUNTU" /etc/os-release; then
        echo "1"
        
    # CentOS
    elif [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then
        echo "2"
        
    # Deepin
    elif [ -f /etc/os-release ] && grep -q "DEEPIN" /etc/os-release; then
        echo "3"
        
    else
        echo "0"
    fi
}

# 判断系统类型
get_os_type=$(get_os)

# 更换镜像源 
update_source() {
    echo "更换镜像源 ...."

    if [ "$get_os_type" == "1" ]; then  
        curl $ubuntu_base | bash

    elif [ "$get_os_type" == "2" ]; then  
        curl $centos_base | bash

    elif [ "$get_os_type" == "3" ]; then  
        echo "未找到源地址"

    else  
        echo "Unknown Linux distribution"
    fi

    # echo "Unknown Linux distribution"
}

# 提示用户是否确认某个操作
echo "是否需要更新镜像源 (y/n)"
read is_update_source
if [[ "$is_update_source" == "y" || "$is_update_source" == "Y" ]]; then

    # 更换镜像源
    update_source
else
    echo "用户取消更新镜像源"
    # exit 0 # 退出
fi

echo "安装工具 ...."
if [ "$get_os_type" == "1" ]; then

    # 安装工具
    apt install -y openssh-client iproute2 rsync sshpass telnet iputils-ping

elif [ "$get_os_type" == "2" ]; then

    # 安装工具
    yum install -y openssh-clients iproute rsync sshpass telnet ping

elif [ "$get_os_type" == "3" ]; then
    echo "This is Deepin"

else
   echo "Unknown Linux distribution"
fi